<?php

namespace app\admin\controller;

use app\admin\model\QuanGuanMemberModel;
use app\admin\services\InstrumentService;
use app\admin\services\DeviceTypeService;
use app\home\services\MemberService;
use think\App;

class InstrumentController extends Common implements ICurd
{
    /**
     * 设备类型
     */
    private $deviceTypeList;

    /**
     * 用户列表
     */
    private $memberList;

    public function initialize()
    {
        parent::initialize();
        $this->setDeviceTypeList();
        $this->setMemberList();
    }

    public function index()
    {
        // TODO: Implement index() method.
        if (request()->isPost()) {
            $requestData = input('post.');
            $param['page'] = $requestData['page'] ?: 1;
            $param['pageSize'] = $requestData['limit'] ?: 10;
            $param['key'] = trim($requestData['key']) ?: "";
            $param['type'] = (int)$requestData['type'] ?: 0;
            // 需要查询的字段
            $field = "
                id,
                title,
                status,
                type,
                create_time,
                current_id
            ";
            $list = $this->getService()->list($param, $field);
            foreach ($list['data'] as &$item) {
                $item['type'] = $this->deviceTypeList[$item['type']]['title'];
                $item['create_time'] = date("Y-m-d H:i:s", $item['create_time']);
                $item['current_id'] = $this->memberList[$item['current_id']]['name'];
            }
            return $this->jsonList($list['data'], $list['total']);
        }
        return $this->fetch("instrument/index");
    }

    public function create()
    {
        // TODO: Implement create() method.
        if (request()->isPost()) {
            $param = $this->mapping();
            $res = $this->getService()->save($param);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        }
        return $this->fetch("instrument/create");
    }

    public function update($id = 0)
    {
        // TODO: Implement update() method.
        if (request()->isPost()) {
            $param = $this->mapping();
            $res = $this->getService()->save($param, $id);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        } else {
            $info = $this->getService()->getInfoById($id);
            $this->assign([
                'info' => json_encode($info, true),
                'infoArr' => $info,
            ]);
            return $this->fetch("instrument/update");
        }
        return $this->jsonError();
    }

    public function remove()
    {
        // TODO: Implement remove() method.
        if (request()->isPost()) {
            $data = input('post.');
            $id = (int)$data['id'];
            $res = $this->getService()->save(['is_delete' => 2], $id);
            if ($res) {
                return $this->jsonSuccess("index");
            }
            return $this->jsonError();
        }
        return $this->jsonError();
    }

    public function detail()
    {
        // TODO: Implement detail() method.
    }

    public function setStatus()
    {
        // TODO: Implement setStatus() method.
        if (request()->isPost()) {
            $id = input('post.id', 0, 'int');
            $status = input('post.status', 0, 'int');
            $res = $this->getService()->save([
                'status' => $status
            ], $id);
            if ($res) {
                return $this->jsonSuccess('index');
            }
            return $this->jsonError();
        }
        return $this->jsonError();
    }

    private function getService()
    {
        return InstrumentService::makeInstance();
    }

    private function mapping()
    {
        $data = input('post.');
        $param = [
            'title' => trim($data['title']) ?: "",
            'status' => (int)$data['status'] ?: 1,
            'sort' => (int)$data['sort'] ?: 0,
            'type' => (int)$data['type'] ?: 0,
        ];
        return $param;
    }

    private function setDeviceTypeList()
    {
        $deviceTypeService = new DeviceTypeService();
        $list = $deviceTypeService->getOpenData();
        $this->deviceTypeList = $list;
        $this->assign([
            'deviceTypeList' => $this->deviceTypeList
        ]);
    }

    private function setMemberList()
    {
        $memberModel = new QuanGuanMemberModel();
        $list = $memberModel->field('id,name')->where([
            'is_delete' => 1
        ])->select()->toArray();
        $this->memberList = array_column($list, null, 'id');
    }

}